package com.controller.backend;

import com.alibaba.fastjson.JSON;
import com.biz.*;
import com.core.Result;
import com.core.ResultGenerator;
import com.entity.model.*;
import com.util.SystemConstant;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Condition;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by jc on 2023/03/15.
 */
@Controller
@RequestMapping("/backend/security/examineeArrange")
public class ExamineeArrangeController extends BaseController<ExamineeArrange> {
    @Autowired
    private ExamineeArrangeBiz examineeArrangeBiz;

    @Autowired
    private ExamineeRecordBiz examineeRecordBiz;

    @Autowired
    private ExamineeArrangeRecordInfoBiz examineeArrangeRecordInfoBiz;

    @Autowired
    private OperationLogBiz operationLogBiz;

    @Autowired
    private ExamineeInfoBiz examineeInfoBiz;

    @RequestMapping(value = "index")
    public String index() throws Exception {
        return "/backend/security/examineeArrange/list";
    }

    @RequestMapping(value = "index2")
    public String index2() throws Exception {
        return "/backend/security/examineeArrange/list2";
    }

    @RequestMapping(value = "detail")
    public String detail() throws Exception {
        return "/backend/security/examineeArrange/form";
    }

    @RequestMapping(value = "list3")
    public String list3() throws Exception {
        return "/backend/security/examineeArrange/list3";
    }

    @RequestMapping(value = "addExamineeArrange")
    @ResponseBody
    public Result addExamineeArrange(HttpSession session, ExamineeArrange examineeArrange) {
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute(SystemConstant.SESSION_ADMIN);
        if (examineeArrange != null) {
            baseBiz.save(examineeArrange);

            OperationLog operationLog = new OperationLog();
            operationLog.setManagerId(managerInfo.getId());
            operationLog.setManagerName(managerInfo.getUserName());
            operationLog.setLogType(3);
            operationLog.setLogRemark("新增考试安排");
            operationLogBiz.save(operationLog);

            return ResultGenerator.genSuccessResult(examineeArrange);
        }
        return ResultGenerator.genFailResult("操作失败");
    }

    /**
     * 更新一个对象
     */
    @RequestMapping(value = "updateExamineeArrange")
    @ResponseBody
    public Result updateExamineeArrange(HttpSession session, ExamineeArrange examineeArrange) {
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute(SystemConstant.SESSION_ADMIN);
        if (examineeArrange != null) {
            baseBiz.update(examineeArrange);

            OperationLog operationLog = new OperationLog();
            operationLog.setManagerId(managerInfo.getId());
            operationLog.setManagerName(managerInfo.getUserName());
            operationLog.setLogType(4);
            operationLog.setLogRemark("修改考试安排");
            operationLogBiz.save(operationLog);

            return ResultGenerator.genSuccessResult(examineeArrange);
        }
        return ResultGenerator.genFailResult("操作失败");
    }


    @RequestMapping(value = "/examineeList")
    @ResponseBody
    public Result examineeList(Integer id) {
        Condition condition = new Condition(ExamineeArrange.class);
        condition.createCriteria().andEqualTo("examineeHallId", id);
        List<ExamineeArrange> examineeArranges = examineeArrangeBiz.findByCondition(condition);
        return ResultGenerator.genSuccessResult(examineeArranges, examineeArranges.size());
    }


    @RequestMapping(value = "importExamineeArrange")
    @ResponseBody
    public Result importExamineeArrange(HttpSession session, @RequestParam String data, @RequestParam String data2, Integer examineeArrangeId) {
        ManagerInfo managerInfo = (ManagerInfo) session.getAttribute(SystemConstant.SESSION_ADMIN);

        List<ExamineeArrangeRecordInfo> list = JSON.parseArray(data,
                ExamineeArrangeRecordInfo.class);
        for (ExamineeArrangeRecordInfo i : list) {
            Condition condition = new Condition(ExamineeArrangeRecordInfo.class);
            condition.createCriteria().andEqualTo("examineeArrangeRecordId", examineeArrangeId).andEqualTo("examineeName", i.getExamineeName()).andEqualTo("idCard", i.getIdCard());
            if (examineeArrangeRecordInfoBiz.findByCondition(condition).size() > 0) {

            } else {
                i.setExamineeArrangeRecordId(examineeArrangeId);
                i.setIsPay(0);
                examineeArrangeRecordInfoBiz.save(i);


                condition = new Condition(ExamineeInfo.class);
                condition.createCriteria().andEqualTo("examineeInstitution", i.getExamineeInstitution()).andEqualTo("examineeName", i.getExamineeName()).andEqualTo("idCard", i.getIdCard());
                if (examineeInfoBiz.findByCondition(condition).size() > 0) {

                } else {
                    ExamineeInfo examineeInfo = new ExamineeInfo();
                    examineeInfo.setAccount(i.getAccount());
                    examineeInfo.setExamineeName(i.getExamineeName());
                    examineeInfo.setExamineeInstitution(i.getExamineeInstitution());
                    examineeInfo.setIdCard(i.getIdCard());
                    examineeInfo.setSex(i.getSex());
                    examineeInfoBiz.save(examineeInfo);
                }
            }
        }

        List<ExamineeArrangeRecordInfo> list2 = JSON.parseArray(data2,
                ExamineeArrangeRecordInfo.class);
        for (ExamineeArrangeRecordInfo j : list2) {
            Condition condition = new Condition(ExamineeArrangeRecordInfo.class);
            condition.createCriteria().andEqualTo("examineeArrangeRecordId", examineeArrangeId).andEqualTo("examineeName", j.getExamineeName()).andEqualTo("idCard", j.getIdCard());
            List<ExamineeArrangeRecordInfo> examineeArrangeRecordInfos = examineeArrangeRecordInfoBiz.findByCondition(condition);
            if (examineeArrangeRecordInfos.size() > 0) {
                ExamineeArrangeRecordInfo examineeArrangeRecordInfo = examineeArrangeRecordInfos.get(0);
                examineeArrangeRecordInfo.setIsPay(1);
                examineeArrangeRecordInfoBiz.update(examineeArrangeRecordInfo);
            }
        }

        OperationLog operationLog = new OperationLog();
        operationLog.setManagerId(managerInfo.getId());
        operationLog.setManagerName(managerInfo.getUserName());
        operationLog.setLogType(4);
        operationLog.setLogRemark("新增考试,导入考生数据");
        operationLogBiz.save(operationLog);

        return ResultGenerator.genSuccessResult("导入成功");
    }
}
